import { ref, computed, watch } from 'vue'
import { defineStore } from 'pinia'
import api from '../api/request'
import { useRoute, useRouter } from 'vue-router';
import commen from '../commen/index';



export const useExamStore = defineStore('exam', () => {
  // 分类数据.............................................
  // 记录考试分类数据
  const category = ref([])
  // 获取考试分类方法
  async function getCategory() {
    await api.category_list()
      .then(res => {
        if (res.code == 200) {
          category.value = res.result
        }
      })
  }
  //   试卷列表...................................................
  const examList: any = ref([])
  // 根据分类获取试卷列表
  function exam_list_by_category(category_id: number) {
    api.exam_list_all()
      .then(res => {
        examList.value = res.result.filter((item: { id: number }) => item.id == category_id)
      })
  }

  // 试卷详情......................................
  const examInfo: any = ref({})
  let time: any = ref();
  let newTime = 0;
  function examInfoFn() {
    const $route = useRoute()
    let eid = $route.query.eid//获取的试卷eid

    api.exam_list({ eid })
      .then(res => {
        examInfo.value = res.result[0];
        newTime = res.result[0].time;
      })
  }
  // 倒计时
  function destoryTimer() {
    let timer: any = null;
    clearInterval(timer);
    timer = setInterval(() => {
      newTime -= 1;
      time.value = commen.procesTime(newTime)
      if (newTime == 0) {
        clearInterval(timer);
      }
    }, 1000);
  }

  // 题目列表..................................................
  let topiclist: any = ref([]);//题目列表
  let index: any = ref(0)//索引
  let newtopiclist: any = ref([]);//根据index的题目列表
  function topiclistFn() {
        //获取的试卷eid
        const $route = useRoute()
        let eid = $route.query.eid
    // 获取题目列表
    api.topic_list({ eid })
      .then(res => {
        if (res.code == 200) {
          // 获取题目列表给newtopiclist
          newtopiclist.value = res.result;
          //   json转数组格式化选项
          for (let i = 0; i < newtopiclist.value.length; i++) {
            newtopiclist.value[i].option = JSON.parse(newtopiclist.value[i].option)
          }
          // 初始
          topiclist.value = newtopiclist.value[index.value];
        }
        
      })

  }
  // 下一题
  function next() {
    index.value += 1;
    if (index.value >= newtopiclist.value.length) {
      index.value = newtopiclist.value.length - 1;
    }
    topiclist.value = newtopiclist.value[index.value];
  }
  // 上一题
  function prev() {
    index.value -= 1;
    if (index.value < 0) {
      index.value = 0;
    }
    topiclist.value = newtopiclist.value[index.value];
  }

  // 成绩.....................................................
  let grade:any =ref({})
  let daan:any=ref([])

  // 返回数据和方法
  return {
    category,
    examList,
    examInfo,
    time,
    topiclist,
    index,
    newtopiclist,
    grade,
    daan,
    // newtopiclistFn,
    next,
    prev,
    topiclistFn,
    exam_list_by_category,
    getCategory,
    examInfoFn,
    destoryTimer,
  }
})
